import requests
import re

urlHeader = "http://data.10jqka.com.cn/ajax/yjyg/date/2021-06-30/board/ALL/field/enddate/order/desc/page/"
urlTail = "/ajax/1/free/1/"
page = 1
totalPage = 0
headers = {
        "Accept": "gzip, deflate",
        "Accept-Encoding": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
        "Accept-Language": "zh-CN,zh-TW;q=0.9,zh;q=0.8,en;q=0.7,en-US;q=0.6",
        "Connection": "keep-alive",
        "hexin-v": "A1PlhHacN_fNRPsjkQOP_ysB4tx-COfKoZwr_gVwr3KphH2Kjdh3GrFsu0wW",
        "Host": "data.10jqka.com.cn",
        "Referer": "http://data.10jqka.com.cn/financial/yjyg/",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest"
    }

totalPageRe = re.compile('<span class="page_info">1/(.*?)</span>')
bodyRe = re.compile('<tbody>(.*?)</tbody>')
rowRe = re.compile('<td>\\d*</td>(.*?)</tr>')
contentRe = re.compile('<tdclass="linkToGghq"><ahref="http://stockpage\.10jqka\.com\.cn/\d*/finance/"target="_blank"class="stockCode">(.*?)</a></td><tdclass="linkToGghq"><ahref="http://stockpage\.10jqka\.com\.cn/\d*/finance/"target="_blank"code="hs_\d*"class="J_showCanvas">(.*?)</a></td><tdclass=".*?"><span>.*?</span></td><tdclass="tl"><ahref="http:.*?"target="_blank">(.*?)</a></td><tdclass=".*?">(.*?)</td><tdclass="c-\\w*">(.*?)</td><tdclass="tccur">(.*?)</td>')


def parseContent(contentStr):
    bodyResultArr = bodyRe.findall(contentStr)
    if len(bodyResultArr) > 0:
        bodyResult = bodyResultArr[0]
        rowResult = rowRe.findall(bodyResult)
        for row in rowResult:
            contentResult = contentRe.findall(row)
            if len(contentResult) > 0:
                print(contentResult)
            else:
                print("======error-row======")
                print(row)
                print("======end======")
    else:
        print("======error-content======")
        print(contentStr)
        print("======end======")


url = urlHeader + str(page) + urlTail
content = requests.get(url, headers=headers).text

totalPageResult = totalPageRe.findall(content)

if len(totalPageResult) > 0:
    totalPage = totalPageResult[0]
    while int(page) <= int(totalPage):
        url = urlHeader + str(page) + urlTail
        reqResult = requests.get(url, headers=headers)
        pageContent = reqResult.text.replace(" ", "").replace("\n", "").replace("\r", "").replace("\t", "")
        parseContent(pageContent)
        page = page + 1